package com.sunday.common.mq.rocket.brave.study.apache.e6_Brave_Trace;

import com.sunday.common.mq.rocket.brave.study.apache.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.stream.Stream;

/**
 * 邮件跟踪
 * https://github.com/apache/rocketmq-spring/wiki/Message-Tracing
 */
@Slf4j
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class BraveTraceApi {

    public static final DateTimeFormatter DATETIME_MS_FORMATTER_SPLIT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");

    final RocketMQTemplate rocketMQTemplate;

    @GetMapping("send")
    public void send() {
        Stream.iterate(0, i -> i < 1, i -> i + 1).forEach(i -> {
            User user = User.builder().id(i).userName(LocalDateTime.now().format(DATETIME_MS_FORMATTER_SPLIT)).userAge(33 + i).build();
            log.info("{}", user);
            rocketMQTemplate.convertAndSend("your_topic", user);
        });
    }

}